package GUI.TabPanels.AdminTabs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import main.ScreenController;

import GUI.Controller.Controller;

@SuppressWarnings("serial")
public class CreateCustomerAccountPanel extends JPanel{
	private ScreenController screen_controller;
	private JLabel customer_firstname_label;
	private JTextField customer_firstname_text;

	private JLabel customer_lastname_label;
	private JTextField customer_lastname_text;

	private JLabel customer_email_label;
	private JTextField customer_email_text;

	private JLabel customer_password_label;
	private JPasswordField customer_password_text;

	private JLabel customer_address_label;
	private JTextField customer_address_text;

	private JLabel customer_phno_label;
	private JTextField customer_phno_text;
	
	private JLabel credit_card_label;
	private JTextField credit_card_text;
	
	private JLabel customer_subscription_label;
	private JCheckBox customer_subscription_checkbox ;
	
	private JButton add_account_button;
	private JButton cancel_button;


	private JPanel topPanel;
	private JPanel middlePanel;


	public CreateCustomerAccountPanel(){
		this.setBackground(Color.PINK);
		screen_controller = ScreenController.getInstance();
		this.setLayout(new BorderLayout());
		this.SetUpTopPanel();
		this.SetUpMiddlePanel();
	}

	public void SetUpTopPanel(){
		topPanel = new JPanel();
		this.add(topPanel, BorderLayout.NORTH);
		topPanel.setLayout(new GridLayout(0, 2));
		topPanel.setBackground(Color.pink);
		
		customer_email_label = new JLabel("Email");
		customer_email_text = new JTextField(9);
		customer_email_text.setEditable(true);
		topPanel.add(customer_email_label);
		topPanel.add(customer_email_text);
		
		customer_password_label = new JLabel("Password");
		customer_password_text = new JPasswordField(9);
		customer_password_text.setEditable(true);
		topPanel.add(customer_password_label);
		topPanel.add(customer_password_text);

		customer_firstname_label = new JLabel("First Name:");
		customer_firstname_text = new JTextField(9);
		customer_firstname_text.setEditable(true);
		topPanel.add(customer_firstname_label);
		topPanel.add(customer_firstname_text);
		
		customer_lastname_label = new JLabel("Last Name:");
		customer_lastname_text = new JTextField(9);
		customer_lastname_text.setEditable(true);
		topPanel.add(customer_lastname_label);
		topPanel.add(customer_lastname_text);
		
		customer_address_label = new JLabel("Address:");
		customer_address_text = new JTextField(9);
		customer_address_text.setEditable(true);
		topPanel.add(customer_address_label);
		topPanel.add(customer_address_text);
		
		customer_phno_label = new JLabel("Phone Number:");
		customer_phno_text = new JTextField(9);
		customer_phno_text.setEditable(true);
		topPanel.add(customer_phno_label);
		topPanel.add(customer_phno_text);
		
		credit_card_label = new JLabel("Credit Card Number");
		credit_card_text = new JTextField(9);
		credit_card_text.setEditable(true);
		topPanel.add(credit_card_label);
		topPanel.add(credit_card_text);
		
		customer_subscription_label = new JLabel("Do you want to subscribe to special offers?");
		customer_subscription_checkbox = new JCheckBox();
		topPanel.add(customer_subscription_label);
		topPanel.add(customer_subscription_checkbox);
	}

	public void SetUpMiddlePanel(){
		JLabel emptyLabel = new JLabel();
		middlePanel = new JPanel();
		middlePanel.setBackground(Color.pink);
		
		add_account_button = new JButton("Add Account");
		add_account_button.addActionListener(new AddCustomerAccountButton());
		
		cancel_button = new JButton("Cancel");
		cancel_button.addActionListener(new CancelButton());

		middlePanel.add(emptyLabel);
		middlePanel.add(add_account_button);
		middlePanel.add(cancel_button);
		middlePanel.add(emptyLabel);

		this.add(middlePanel, BorderLayout.CENTER);
	}


	private class AddCustomerAccountButton implements ActionListener{

		@SuppressWarnings("deprecation")
		@Override
		public void actionPerformed(ActionEvent arg0) {
			try
			{
				// String[] customerParams = { "LastName", "FirstName", "PsWd", "email",
				// "0", "address","4038888888", "0" };
				if (customer_lastname_text.getText().compareTo("") == 0 
						|| customer_lastname_text.getText().length() < 2
						|| customer_firstname_text.getText().compareTo("") == 0 
						|| customer_firstname_text.getText().length() < 2
						|| customer_password_text.getText().compareTo("") == 0 
						|| customer_password_text.getText().length() < 6
						|| customer_email_text.getText().compareTo("") == 0 
						|| customer_email_text.getText().length() < 2
						|| customer_address_text.getText().compareTo("") == 0 
						|| customer_address_text.getText().length() < 2
						|| customer_phno_text.getText().compareTo("") == 0 
						|| customer_phno_text.getText().length() < 2)
					throw new Exception();
				Long.parseLong(customer_phno_text.getText());
				Long.parseLong(credit_card_text.getText());
				String[] data = {
						customer_lastname_text.getText(),
						customer_firstname_text.getText(),
						customer_password_text.getText(),
						customer_email_text.getText(),
						"0",
						customer_address_text.getText(),
						customer_phno_text.getText(),
						(customer_subscription_checkbox.isSelected() ? "0" : "1")
				};

				Controller.CreateCustomer(data);
				JOptionPane.showMessageDialog(null, "Account Created!");
				screen_controller.ViewLoginScreen();

			}
			catch(NumberFormatException e)
			{
				JOptionPane.showMessageDialog(null, "Room Number must be an integer.");
			}
			catch(Exception e){
				JOptionPane.showMessageDialog(null, "Fields cannot be empty or less than 2 characters. Passwords must be at least 6 characters long.");
			}
		}
	}
	private class CancelButton implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			screen_controller.ViewLoginScreen();
		}
	}
}
